<template>
  <a-layout>
    <a-layout-sider width="200" style="background: #fff">
      <a-menu
              mode="inline"
              :style="{ height: '100%', borderRight: 0 }"
              @click="handleClick"
              :openKeys="openKeys"
      >
        <a-menu-item key="welcome">
          <MailOutlined />
          <span>欢迎</span>
        </a-menu-item>
        <a-sub-menu v-for="item in categoryLevels" :key="item.id" >
          <template v-slot:title>
          <span><user-outlined />{{item.name}}</span>
        </template>
          <a-menu-item v-for="child in item.children" :key="child.id">
            <MailOutlined /><span>{{child.name}}</span>
          </a-menu-item>
        </a-sub-menu>
        <a-menu-item key="tip" :disabled="true">
          <span>以上菜单在分类管理配置</span>
        </a-menu-item>

      </a-menu>
    </a-layout-sider>
    <a-layout style="padding: 0 24px 24px">
      <a-layout-content
              :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
      >
        <div v-show="isShowWelcome">
          <the-home></the-home>
        </div>
        <a-list v-show="!isShowWelcome" item-layout="vertical" size="large" :grid="{gutter: 16, column: 3}" :data-source="ebooks">
          <template #renderItem="{ item }">
            <a-list-item key="item.title">
              <template #actions>
                <span>
                <component v-bind:is="'FileOutlined'" style="margin-right: 8px" />
                {{ item.docCount }}
              </span>
                <span>
                <component v-bind:is="'UserOutlined'" style="margin-right: 8px" />
                {{ item.viewCount }}
              </span>
                <span>
                <component v-bind:is="'LikeOutlined'" style="margin-right: 8px" />
                {{ item.voteCount }}
              </span>
              </template>
              <a-list-item-meta :description="item.description">
                <template #title>
                  <router-link :to="'/doc?ebookId=' + item.id">
                    {{ item.name }}
                  </router-link>
                </template>
                <template #avatar><a-avatar :src="item.cover" /></template>
              </a-list-item-meta>
            </a-list-item>
          </template>
        </a-list>
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>

<script lang="ts">
  import {FileOutlined,MailOutlined,StarOutlined, LikeOutlined, MessageOutlined,UserOutlined, LaptopOutlined, NotificationOutlined } from '@ant-design/icons-vue';
  import { } from '@ant-design/icons-vue';
  import { defineComponent, ref,onMounted } from 'vue';
  import axios from 'axios';
  import {Tool} from "@/util/tool";
  import TheHome from '@/components/the-home.vue';
  const ebooks = ref();//定义一个响应式的对象
  export default defineComponent({
    components: {
      UserOutlined,
      LaptopOutlined,
      NotificationOutlined,
      StarOutlined,
      LikeOutlined,
      MessageOutlined,
      MailOutlined,
      FileOutlined,
      TheHome
    },
    setup() {
      const isShowWelcome = ref(true);
      let categoryId2 = 0;
      const  openKeys =  ref();
      //进行异步请求获取后台数据
      const handleQueryEbook = () => {
        axios.get("/ebook/getAll", {
          params: {
            page: 1,
            size: 1000,
            category2Id: categoryId2
          }
        }).then((response) => {
          //接收返回的数据
          const result = response.data;
          console.log("后台返回的数据："+result);
          if(result.success){//请求成功
            ebooks.value = response.data.content;//给响应式的变量赋值
          }
        });
      };
      /**
       * 查找分类
       */
      const categoryLevels =  ref();
      let categorys: any;
      //进行异步请求获取后台数据
      const handleQueryCategory = ()=>{
        axios.get("/category/getAll").then(function (response) {
          //接收返回的数据
          const result = response.data;
          if(result.success){//请求成功
            categorys = response.data.content;//给响应式的变量赋值
            console.log("后台返回的数据："+categorys);
            // 加载完分类后，将侧边栏全部展开
            openKeys.value = [];
            for (let i = 0; i < categorys.length; i++) {
              openKeys.value.push(categorys[i].id)
            }
            categoryLevels.value = [];
            categoryLevels.value = Tool.array2Tree(categorys,0);//获取一级分类及其子分类
            console.log("转换后的数据："+categoryLevels.value);
            handleQueryEbook();
          }
        });
      };
      const handleClick = (value: any) => {
        //console.log("menu click", value)
        if (value.key === 'welcome') {
          isShowWelcome.value = true;
        } else {
          categoryId2 = value.key;
          isShowWelcome.value = false;
          handleQueryEbook();
        }
        // isShowWelcome.value = value.key === 'welcome';
      };
      onMounted(() => {
        handleQueryCategory();
      });
      return {
        ebooks,
        handleClick,
        categoryLevels,
        isShowWelcome,
        openKeys
      };
    }
  });
</script>
<style scoped>
  .ant-avatar {
    width: 50px;
    height: 50px;
    line-height: 50px;
    border-radius: 8%;
    margin: 5px 0;
  }
</style>
